#include "stack.h"
#include "stdlib.h"

STATUS Push(Stack *s, int data)
{
    if (s->cur > s->cap - 1) {
        return RET_STATUS_ERR;
    }
    *(s->head + s->cur) = data;
    s->cur++;
    return RET_STATUS_OK;
}

STATUS Pop(Stack *s, int *e)
{
    if (s->cur == 0) {
        return RET_STATUS_ERR;
    }
    *e = s->head[--s->cur];
    return RET_STATUS_OK;
}

STATUS Top(Stack *s, int *e)
{
    if (s->cur == 0) {
        return RET_STATUS_ERR;
    }
    *e = s->head[s->cur - 1];
    return RET_STATUS_OK;
}

Stack *NewStack()
{
    int cap = MAX_STACK_POOL_SIZE;

    Stack *s = (Stack *)malloc(sizeof(Stack));
    if (s == NULL) {
        return NULL;
    }
    int *pool = (int *)malloc(sizeof(int) * cap);
    if (pool == NULL) {
        free(s);
        return NULL;
    }
    s->cap  = cap;
    s->cur  = 0;
    s->head = pool;
    return s;
}
STATUS DestoryStack(Stack *s)
{
    s->cur = 0;
    s->cap = 0;
    free(s->head);
    free(s);
    return RET_STATUS_OK;
}
